package com.scan.plugin.plus.scanPluginPlus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.barcode.barcode.BarcodeUtility;

/**
 * Created by wskyo on 2023-9-4.
 */

public class Barcode2D {
    private String TAG = "Scanner_barcodeTest";
    private BarcodeUtility barcodeUtility = null;
    private BarcodeDataReceiver barcodeDataReceiver = null;
    private IBarcodeResult iBarcodeResult = null;
    private Context context;
    private int scanner_type = 0; // 0 ,1 硬解 ; 2, 软 解

    private static final String ACTION_BARCODE_NOT_REPEAT_6 = "android.intent.action.BARCODE_NOT_REPEAT";
    private static final String ACTION_BARCODE_NOT_REPEAT_new = "com.barcode.scanner.action.BARCODE_NOT_REPEAT";

    public Barcode2D(Context context) {
        barcodeUtility = BarcodeUtility.getInstance();
        this.context = context;
        scanner_type = Settings.Global.getInt(context.getContentResolver(), "Hardware_type", 0);
    }

    //开始扫码
    public void startScan(Context context) {
        if (barcodeUtility != null) {
            Log.i(TAG, "ScanBarcode , scanner_type = " + scanner_type);
            if (scanner_type == 2) {
                barcodeUtility.startScan(context, BarcodeUtility.ModuleType.BARCODE_2D);
            } else {
                barcodeUtility.startScan(context, BarcodeUtility.ModuleType.BARCODE_1D);
            }
        }
    }
    public boolean openScan(){
        startScan(context);
        return true;
    }

    //停止扫描
    public void stopScan(Context context) {
        if (barcodeUtility != null) {
            Log.i(TAG, "stopScan , scanner_type = " + scanner_type);
            if (scanner_type == 2) {
                barcodeUtility.stopScan(context, BarcodeUtility.ModuleType.BARCODE_2D);
            } else {
                barcodeUtility.stopScan(context, BarcodeUtility.ModuleType.BARCODE_1D);
            }
        }
    }
    public boolean closeScan(){
        stopScan(context);
        return true;
    }

    //打开
    public void open(Context context, IBarcodeResult iBarcodeResult) {
        if (barcodeUtility != null) {
            this.iBarcodeResult = iBarcodeResult;
            barcodeUtility.setOutputMode(context, 2);//设置广播接收数据
            barcodeUtility.setScanResultBroadcast(context, "com.scanner.broadcast", "data");//设置接收数据的广播
            if (scanner_type == 2) {
                barcodeUtility.open(context, BarcodeUtility.ModuleType.BARCODE_2D);
            } else {
                context.sendBroadcast(new Intent("com.scanner.broadcast.open hardware"));
                barcodeUtility.open(context, BarcodeUtility.ModuleType.BARCODE_1D);
            }
            barcodeUtility.setReleaseScan(context, false);//设置松开扫描按键，不停止扫描
            barcodeUtility.setScanFailureBroadcast(context, true);//扫描失败也发送广播
            //barcodeUtility.setContinuousScanIntervalTime(context, 2);//设置连续扫描间隔时间
            barcodeUtility.enableContinuousScan(context, false);//关闭扫描助手连续扫描
            barcodeUtility.enablePlayFailureSound(context, false);//关闭扫描助手 扫描失败的声音
            //barcodeUtility.enablePlaySuccessSound(context, false);//关闭扫描助手 扫描成功的声音
            barcodeUtility.enableEnter(context, false);//关闭回车
            barcodeUtility.setBarcodeEncodingFormat(context, 1);

            enableBarcodeNotRepeat(context, false);//关闭条码不重复显示

            if (barcodeDataReceiver == null) {
                barcodeDataReceiver = new BarcodeDataReceiver();
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction("com.scanner.broadcast");
                context.registerReceiver(barcodeDataReceiver, intentFilter);
            }
        }
    }

    //关闭
    public void close(Context context) {
        if (barcodeUtility != null) {
            if (scanner_type == 2) {
               // barcodeUtility.close(context, BarcodeUtility.ModuleType.BARCODE_2D);
            } else {
                barcodeUtility.close(context, BarcodeUtility.ModuleType.BARCODE_1D);
            }
            if (barcodeDataReceiver != null) {
                context.unregisterReceiver(barcodeDataReceiver);
                barcodeDataReceiver = null;
            }
        }
    }

    // 开启/关闭连续扫描
    public void enableContinuousScan(boolean enable){
        barcodeUtility.enableContinuousScan(context, enable);
    }
    public void startContinuousScan(){
        barcodeUtility.enableContinuousScan(context, true);
    }
    public void stopContinuousScan(){
        barcodeUtility.enableContinuousScan(context, false);
    }

    //设置扫描输出模式  0:模拟按键    1:剪切板   2:广播  3:模拟按键
    public void setOutScanMode(int mode)
    {
        barcodeUtility.setOutputMode(context, mode);
    }

    //开启/关闭 拦截扫描键
    public void enableBlockScankey(boolean enable){
        barcodeUtility.enableBlockScankey(context,enable);
    }
    // 暂不支持
    public int getOutScanMode(){
        return -1;
    }
    //设置声音输出开关
    public void setScanSound(boolean on)
    {
        barcodeUtility.enablePlaySuccessSound(context,on);
    }
    //设置振动输出开关
    public void setScanVibrator(boolean on)
    {
        barcodeUtility.enableVibrate(context, on);
    }
    //设置回车输出开关
    public void setScanEnterKey(boolean on)
    {
        barcodeUtility.enableEnter(context, on);
    }
    //设置前缀字符
    public void setScanPrefix(String prefix)
    {
        barcodeUtility.setPrefix(context, prefix);
    }
    //设置后缀字符
    public void setScanSuffix(String suffix)
    {
        barcodeUtility.setSuffix(context, suffix);
    }
    //设置扫描Laser模式
    public void setScanLaserMode(int mode)
    {
        if (mode == 4) {
            barcodeUtility.enableContinuousScan(context, true);
        } else if (mode == 8) {
            barcodeUtility.enableContinuousScan(context, false);
        }

    }

    public void enableBarcodeNotRepeat(Context context, boolean enable) {
        if (context == null) return;
        Log.i(TAG, "output repeated barcode: " + enable + ", android_ver = " + Build.VERSION.SDK_INT);
        String action;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // Android 9.0及以上
            action = ACTION_BARCODE_NOT_REPEAT_new;
        }
        else {
            // Android 9.0以下
            action = ACTION_BARCODE_NOT_REPEAT_6;
        }
        Intent intent = new Intent(action);
        intent.putExtra("enable", enable);
        intent.setPackage("com.barcode.scanner");
        context.sendBroadcast(intent);
    }

    public static final String ACTION_CONNECT_POWERDOWN = "com.example.uhf_demo.other.pangling_prot.oPowerDown";
    public static final String ACTION_DISCONNECT_POWER = "com.example.uhf_demo.MyApplication.Mreader.CloseReader";

    protected class BarcodeDataReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String barCode = intent.getStringExtra("data");
            String status = intent.getStringExtra("SCAN_STATE");

            Log.i(TAG, "data = " + barCode + ", status=" + status);

            if(intent.getAction().equals(ACTION_CONNECT_POWERDOWN))
            {
                close(context);
            }
            if (status != null && (status.equals("cancel"))) {
                return;
            } else {
                if (barCode == null) {
                    barCode = "Scan fail";
                }
                if (iBarcodeResult != null)
                    iBarcodeResult.getBarcode(barCode);
            }
        }
    }
}


 //if (barCode != null && !barCode.equals("")) {
        //success
        //byte[] barcodeBytes = intent.getByteArrayExtra("dataBytes");//获取原始的bytes数据
        //if(barcodeBytes!=null) {
        //    byte[] decodeData= Base64.decode(barcodeBytes,Base64.DEFAULT);
        //    barCode=StringUtility.bytes2HexString(decodeData);
        //}

    //}
